package cn.casc.tools.maven;

import com.alibaba.fastjson.JSON;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author max
 * @version 1.0.1
 * @className ConfigDialog
 * @description
 * @copyright Copyright (c) 2023
 * @company 天纲梦工厂
 * @datetime 2024/7/18 13:54
 **/
enum ConfigDialog {
    INSTANCE;
    private JDialog dialog = new JDialog();
    public JTextArea textArea = new JTextArea();
    ConfigDialog() {
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setTitle("配置信息");
        dialog.setLayout(new BorderLayout());
        JScrollPane scrollPane = new JScrollPane(textArea);
        textArea.setTabSize(2);
        dialog.add(scrollPane, BorderLayout.CENTER);
        dialog.setLocation(200,100);
        dialog.setSize(500,300);
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                try {
                    Config config = JSON.parseObject(textArea.getText(),Config.class);
                    config.save();
                    Config.load();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(dialog, ex.getMessage(), "配置错误", JOptionPane.ERROR_MESSAGE);
                }

            }
        });
    }
    public void show() {
        textArea.setText(JSON.toJSONString(Config.get(), true));
        this.dialog.setVisible(true);
    }

}